<?php

namespace app\command;

use app\common\lib\Mail;
use app\common\model\Order as OrderModel;
use Redis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\facade\Config;
use think\facade\Db;
use Exception;
use think\facade\Env;
use think\facade\Log;

class Test extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Test')
            ->addArgument('type', Argument::OPTIONAL, "类型")
            ->setDescription('Say Hello');
    }

    /**
     * @throws \think\db\exception\DbException
     */
    protected function execute(Input $input, Output $output)
    {
        try {
            $type = trim($input->getArgument('type'));

            switch ($type) {
                case 1:
                    $this->echo_log('类型' . $type . '，随机6个数字');
                    $arr_num = [];
                    $str_num = '';
                    for ($i = 0; $i < 99; $i++) {
                        $num = mt_rand(1, 80);
                        if (!in_array($num, $arr_num)) {
                            $arr_num[] = $num;
                        }
                        if (count($arr_num) == 6)
                            break;
                    }
                    sort($arr_num);
                    for ($i = 0; $i < count($arr_num); $i++) {
                        $str_num .= ' ' . $arr_num[$i];
                    }
                    $this->echo_log($str_num);

                    break;
                case 2:
                    $code = 1234;
                    $email = "2321588142@qq.com";
                    //$mail = new Mail('formalverification@163.com', 'SYECAWFLLTUPMDRP');
                    $mail = new Mail('389123492@qq.com', 'yaqpblmptyisbgic');
                    $ret = $mail->sendEMail([$email], '验证码', "您的验证码是{$code}" . date('Y-m-d H:i:s'), '如果邮件客户端不支持HTML则显示此内容');

                    var_dump($ret);
                    break;
                default:
                    $this->echo_log('类型' . $type . '，错误的类型方式');
            }


        } catch (Exception $e) {
            Db::rollback();
            // 回滚事务
            $this->echo_log($e->getMessage() . $e->getFile() . $e->getLine());
            $this->echo_log("接口错误，请检查日志请求信息");
        }
    }

    private function echo_log($text)
    {
        Log::record("【自定义】" . $text);
        $this->output->writeln('【自定义】' . $text);
    }
}
